
//
//  MaxAmountView.swift
//  Speek
//
//  Created by ZhenyanShan on 2023/6/30.
//


import SwiftUI
import CoreData

struct MaxAmountView: View{
    @Binding var year : String
    @Binding var month : String
    @Binding var tag: Bool
    
    @Environment(\.managedObjectContext)  var context
    
    @State private var billArray : [BillDetail] = []
    @State private var bill : BillDetail? = nil
    
    @Binding var analysisData: AnalysisData

        var body: some View{
            VStack {
                HStack{
                    if tag{
                        Text("本月最高单笔收入")
                            .font(.headline)
                    }else{
                        Text("本月最高单笔支出")
                            .font(.headline)
                    }
                }
                .padding(.vertical,15)
                VStack{
                    //最大消费账单信息
                    if let bill = bill {
                        //日期
                        Text(formatDateMD(date:bill.date ?? Date()))
                            .padding(.bottom,10)
                        
                        //事件
                        Text(bill.name ?? "")
                            .bold()
                        
                        //种类+金额
                        HStack(spacing: 10){
                            Image(bill.type ?? "其他")
                            Text(getInOrOut(tag:tag)+"¥"+String(bill.cost))
                                .padding(10)
                                .foregroundColor(tag ? Color(red:0,green: 0.725,blue: 0.678):Color(red:100,green: 0.3412,blue: 0.2))
                                .background(RoundedRectangle(cornerRadius: 10)
                                    .stroke(tag ? Color(red:0,green: 0.725,blue: 0.678).opacity(0.5):Color(red:100,green: 0.3412,blue: 0.2).opacity(0.5), lineWidth: 2))
                        }
                    }else{
                        if tag{
                            Text("本月没有收入哦～")
                                .padding(.top,30)
                        }else{
                            Text("本月没有支出～")
                                .padding(.top,30)
                        }
                    }
                    Spacer()
                }
            }
            .padding(.bottom,10)
            .frame(width: 195,height: 200)
            .foregroundColor(Color.white)
            .onAppear{
                billArray = fetchBillDetails(year: year, month: month, tag: tag)
                bill = getMaximumAmount(billArray: billArray)
                
                analysisData.maxAmountBill = getMaximumAmount(billArray: fetchBillDetails(year: year, month: month, tag: false)) ?? BillDetail()
            }
            .onChange(of: year, perform: { _ in
                billArray = fetchBillDetails(year: year, month: month, tag: tag)
                bill = getMaximumAmount(billArray: billArray)
                
                analysisData.maxAmountBill = getMaximumAmount(billArray: fetchBillDetails(year: year, month: month, tag: false)) ?? BillDetail()
            })
            .onChange(of: month, perform: { _ in
                billArray = fetchBillDetails(year: year, month: month, tag: tag)
                bill = getMaximumAmount(billArray: billArray)
                
                analysisData.maxAmountBill = getMaximumAmount(billArray: fetchBillDetails(year: year, month: month, tag: false)) ?? BillDetail()
            })
            .onChange(of: tag, perform: { _ in
                billArray = fetchBillDetails(year: year, month: month, tag: tag)
                bill = getMaximumAmount(billArray: billArray)
                
                analysisData.maxAmountBill = getMaximumAmount(billArray: fetchBillDetails(year: year, month: month, tag: false)) ?? BillDetail()
            })
            .background(Color(red: 0.04, green: 0.09, blue: 0.16).cornerRadius(20))
            .preferredColorScheme(.dark)
        }
    
    //从tag中获取金额显示的正负
    func getInOrOut(tag:Bool)->String{
        if tag {
            return "+"
        }else{
            return "-"
        }
    }
    
    func formatDateMD(date: Date) -> String {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "M月yy日"
            return dateFormatter.string(from: date)
    }
    
    func getMaximumAmount(billArray:[BillDetail]) -> BillDetail?{
        guard let maxAmountBill = billArray.max(by: { $0.cost < $1.cost }) else { return nil }
        return maxAmountBill
    }
}



//
//struct MaxAmountView_Previews: PreviewProvider {
//
//    static var previews: some View {
//        MaxAmountView()
//    }
//}
